/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "TimelineItem.h"
#include "DiagramEdge.h"
#include "DiagramView.h"

namespace QSThink
{
	TimelineItem::TimelineItem( DiagramView *view)
		: _view(view)
	{
		setFlag(ItemIsSelectable);//ItemIsMovable
		setFlag(ItemSendsGeometryChanges);
		setCacheMode(DeviceCoordinateCache);
		setAcceptHoverEvents(false);

		setZValue(2);
	}

	

	QRectF TimelineItem::boundingRect() const
	{
		int width=_view->maxWidth();

		
		return QRectF(0,0,width, 20);

	}


	QPainterPath TimelineItem::shape() const
	{
		QPainterPath path;
		path.addRect(boundingRect());
		return path;
	}

	void TimelineItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
	{
		painter->setBrush(Qt::darkGray);
		//painter->setPen(QPen(Qt::black,0));
		painter->drawRect(boundingRect());

	
		painter->setPen(Qt::black);
		int cnt=_trans.size();
		std::map<int,TransactionPos>::iterator it=_trans.begin();
		for(;it!=_trans.end();++it){
			int year=it->first;
			TransactionPos tp=it->second;
			int x=tp.pos;
			painter->drawLine(x,0,x,20);
			painter->drawText(x+4,16,QString::number(year));


		}
	}

	QVariant TimelineItem::itemChange( GraphicsItemChange change, const QVariant &value )
	{
		

		return QGraphicsItem::itemChange(change, value);


	}

	void TimelineItem::mousePressEvent( QGraphicsSceneMouseEvent *evt )
	{
		update();
		QGraphicsItem::mousePressEvent(evt);

	}

	void TimelineItem::mouseReleaseEvent( QGraphicsSceneMouseEvent *evt )
	{
		update();
		QGraphicsItem::mouseReleaseEvent(evt);

	}

	

	TimelineItem::~TimelineItem()
	{
		
	}

	//assume added must seq
	void TimelineItem::addTrans( float x, Transaction* trans )
	{

		Transaction::Type t=trans->getType();
		switch(t){
			case Transaction::Transaction_Single:
				break;
			case Transaction::Transaction_2Minutes:
				break;
			case Transaction::Transaction_5Minutes:
				break;
			case Transaction::Transaction_30Minutes:
				break;
			case Transaction::Transaction_1Hour:
				break;
			case Transaction::Transaction_1Day:
				{
					Transaction1Day* t=(Transaction1Day*)trans;
					int day=t->day();
					QDate date=Utilities::getDateByDay(day);
					int year=date.year();
					
					std::map<int,TransactionPos>::iterator it=_trans.find(year);
					if(it!=_trans.end()){
						TransactionPos pos=it->second;
						Transaction1Day* t0=(Transaction1Day*)pos.trans;
						if(day<t0->day()){
							it->second=TransactionPos(x,trans);
						}
					}else{
						_trans.insert(std::map<int,TransactionPos>::value_type(year,TransactionPos(x,trans)));
					}

					break;
											   
				}
		}

		_positions.push_back(TransactionPos(x,trans));

	}


}
